<?php


namespace app\admin\controller;


use app\common\controller\Clientvalidate;
use app\common\model\Article;
use app\common\model\Client;
use app\common\validate\Vld_client;
use think\exception\ValidateException;

class Clients extends Base
{

    public  function  get_detail()
    {//获取客户详细信息
        $id=input('post.id');
        $data = Client::field(' id,name,nickName,tel,avatarUrl,address,fst_id,role,input_date ')
            ->where('id',$id)
            ->find();
        //返回判断
        if ($data){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$data,
            'meta'=>$meta
        ];
        return json($res);
    }

    public  function  get_client_list()
    {//栏目列表方法
        $pagenum=input('post.pagenum'); //// 当前页码数
        $pagesize=input('post.pagesize'); //// 当前每页有多少条
        $list = Client::field(' id ,name,avatarUrl,nickname,avatarurl,tel, input_date ')
            ->where('name','not null')
            ->order(['id'=>'desc'])
            ->paginate([
                'list_rows'=> $pagesize,
                'page' => $pagenum,
            ])
            ->toArray();
        //返回判断
        if ($list){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$list,
            'meta'=>$meta
        ];
        return json($res);
    }

    public  function  edit()
    {//修改客户信息
        $data=[
            'id'=>input('post.id'),
            'fst_id'=>input('post.fst_id')
        ];
        try
        {
            $validateres = validate(Vld_client::class)
                ->scene('edit')
                ->check($data);
            if( $validateres)
            {//通过验证
                $mydata = Client::where('id',$data['id'])
                    ->find();
                $result= $mydata->save($data);
                if($result){
                    //判断添加是否成功
                    $msg="ok";
                    $status=200;
                }else{
                    $msg="fail";
                    $status=0;
                }
            }
        }
        catch (ValidateException  $e) {
            // 验证失败 输出错误信息
            $msg=  $e->getMessage();
            $status=0;
        }
        // 返回客户端信息
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>null,
            'meta'=>$meta
        ];
        return json($res);
    }
}
